package com.hellogood.yaohuo.server.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.hellogood.yaohuo.server.db.DBConnection;
import com.hellogood.yaohuo.server.db.DBHelper;
import com.hellogood.yaohuo.shared.entity.User;

public class UserDAO extends DBHelper {
	Logger log = Logger.getLogger(this.getClass());
	String hxDbName = DBConnection.hxDbName;

	@Override
	public List<?> initializeObject(ResultSet rs) throws SQLException {
		List<User> list = new ArrayList<User>();
		if (rs != null) {
			while (rs.next()) {
				User user = new User();
				String staffCode = rs.getString("StaffCode");
				if (staffCode != null) {
					staffCode = staffCode.trim();
				}
				String staffName = rs.getString("StaffName");
				if (staffName != null) {
					staffName = staffName.trim();
				}
				user.setStaffCode(staffCode);
				user.setStaffName(staffName);
				user.setDepCode(rs.getString("DepCode"));
				list.add(user);
			}
		}
		return list;
	}

	public User login(String username, String password) {
		hxDbName = DBConnection.hxDbName;
		String sql = "SELECT staffCode,staffName,depCode FROM " + hxDbName + "..Staffs WHERE StaffCode = ? and Passwd = ?;";
		Object[] args = new Object[] { username, password };
		List<?> list = super.query(sql, args);
		if (list.isEmpty()) {
			return null;
		}
		return (User) list.get(0);
	}

	public List<User> getUngroupedUser() {
		String sql = "SELECT staffCode,staffName,depCode FROM " + hxDbName + "..Staffs WHERE staffCode NOT IN (SELECT staffCode FROM GroupUser) ORDER BY StaffCode";
		List<?> list = super.query(sql);
		return (List<User>) list;
	}

	@SuppressWarnings("unchecked")
	public List<User> getAllUser() {
		String sql = "SELECT staffCode,staffName,depCode FROM " + hxDbName + "..Staffs ORDER BY StaffCode";
		List<?> list = super.query(sql);
		return (List<User>) list;
	}

	public User getUserByCode(String code) {
		String sql = "SELECT staffCode,staffName,depCode FROM " + hxDbName + "..Staffs WHERE StaffCode = '" + code + "'";
		List<?> list = super.query(sql);
		if (list != null && list.size() > 0) {
			return (User) list.get(0);
		}
		return null;
	}

	// 检验用户名和密码是否匹配
	public User checkLogin(String staffCode, String passwd) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		conn = DBConnection.getInstance().getConnection();
		hxDbName = DBConnection.hxDbName;
		String sql = "select staffCode,staffName,depCode from " + hxDbName + "..Staffs where StaffCode = '" + staffCode.trim() + "' and Passwd = '" + passwd + "'";
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			User user = null;
			while (rs.next()) {
				user = new User();
				user.setStaffCode(rs.getString("staffCode") == null ? "" : rs.getString("staffCode").trim());
				user.setStaffName(rs.getString("staffName") == null ? "" : rs.getString("staffName").trim());
				user.setDepCode(rs.getString("depCode") == null ? "" : rs.getString("depCode").trim());
			}
			if (user != null) {
				return user;
			}
			sql = "select staffCode,staffName,depCode from " + hxDbName + "..Staffs where StaffCode = '" + staffCode.trim() + "' and Passwd IS NULL";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				user = new User();
				user.setStaffCode(rs.getString("staffCode") == null ? "" : rs.getString("staffCode").trim());
				user.setStaffName(rs.getString("staffName") == null ? "" : rs.getString("staffName").trim());
				user.setDepCode(rs.getString("depCode") == null ? "" : rs.getString("depCode").trim());
			}
		} catch (SQLException e) {
			e.printStackTrace();
			log.error(e.getMessage());
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (conn != null) {
				// ConnectionPool.getInstance().release(conn);
			}
		}

		return null;
	}
}
